package com.k.base.jdk.thread.thread;

import com.k.base.jdk.thread.ThreadFactoryBuilder;

import java.util.concurrent.*;
import java.util.function.Supplier;

/**
 * @author kivil
 */
public class ThreadTemplate extends ThreadPoolExecutor {

    public ThreadTemplate(){
        super(
                4,
                5,
                3000,
                TimeUnit.MILLISECONDS,
                new ArrayBlockingQueue<>(5)
        );
    }

    public ThreadTemplate(String name){
        super(
                // 核心线程
                5,
                // 最大线程
                3000,
                // 10(maximumPoolSize) -5(corePoolSize) = 5(新建的线程) 这 5个新建的线程 ,空闲时间(keepAliveTime),退出。
                5, TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<>(32),
                new ThreadFactoryBuilder().setNameFormat(name).setDaemon(false).build(),
                new ThreadPoolExecutor.AbortPolicy());
    }

    public ThreadTemplate(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
    }

    public ThreadTemplate(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory);
    }

    public ThreadTemplate(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, RejectedExecutionHandler handler) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, handler);
    }

    public ThreadTemplate(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, handler);
    }


    public <U> CompletableFuture<U>  executeTask(Supplier<U> supplier){
        return CompletableFuture.supplyAsync(supplier,this);
    }
}
